package com.helloae.chap05;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureCursorProxy;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.ITableProxy;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 查找Change字段中大于20的所有要素，将其的Class值修改为101。<br>
 * @author zhushijie
 *
 */
public class UpdateValue {

	public static void main(String[] args) throws UnknownHostException, IOException {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

		//使用ShapefileWorkspaceFactory.openFromFile的方法创建一个Workspace
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap05", 0);
		//加载图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("idcounty");
		
		//设置过滤条件
		QueryFilter pQFilter = new QueryFilter();
		pQFilter.setWhereClause("Change > 20");
		
		//IFeatureClass没有update方法，所以需要向上转型为ITable，然后再由ICursor转型为IFeatureCursorProxy
		IFeatureCursor pUpdateFeatures = new IFeatureCursorProxy(new ITableProxy(pFeatureClass).update(pQFilter, false));
		
		int indexClass = pUpdateFeatures.findField("Class");
		IFeature pFeature = pUpdateFeatures.nextFeature();
		while(pFeature != null){
			pFeature.setValue(indexClass, 101);
			pUpdateFeatures.updateFeature(pFeature);
			pFeature = pUpdateFeatures.nextFeature();
		}
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
